<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TaskController extends BaseController
{
    public function index(Request $request)
    {
        $param = $this->validate($request,[
            'task_type' => 'required|in:1,2'
        ]);


        $user_id = auth('api')->id();
        $query = DB::table('tasks');
            if ($param['task_type']==1){
                $start_time = date('Y-m-d 00:00:01');
                $end_time = date('Y-m-d 23:59:59');
                $query->selectRaw("*,(select count(*) from user_tasks where finish_time between '{$start_time}' and '{$end_time}' and task_id=tasks.id and user_id=$user_id) as task_status");

            }else{

                $query->selectRaw('*,(select exists(select * from user_tasks where task_id=tasks.id and user_id='.$user_id.')) as task_status ');

            }



        $tasks =  $query->where('task_type',$param['task_type'])
                     ->where('status',1)
                     ->get();

        return $this->success($tasks);
    }
}
